#!/bin/sh
set -eu

usage () {
    echo "usage: git workon <branch>" >&2
}

if [ $# -eq 1 ]; then
    branch=$1
else
    usage
    exit 2
fi

if git local-branch-exists "$branch"; then
    git checkout "$branch"
else
    git fetch origin
    if git remote-branch-exists origin "$branch"; then
        git checkout -b "$branch" "origin/$branch"
    else
        echo "Unknown branch '$branch'." >&2
        exit 1
    fi
fi

# Set upstream if remote tracking branch exists
if git remote-branch-exists origin "$branch"; then
    git branch -u "origin/$branch" "$branch"
fi
